package com.books.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
public class ReaderUtil {

    public static void main(String[] args) {
        try {
            String url = "http://www.4xiaoshuo.info/168/168727/";
            Map<String, Object> bookMap = JsoupHtmlUtil.catalogList(url);
            StringBuilder content = new StringBuilder();

            LinkedHashMap<String, String> catalogMap = (LinkedHashMap<String, String>) bookMap.get("catalogMap");
            System.out.println(catalogMap.size());
            catalogMap.forEach((k, v) -> {
//                if (k.equals("55921373.html") || k.equals("55921374.html") || k.equals("55921375.html")) {
                System.out.println(k + "===" + v);
                content.append("\r\n").append(v).append("\r\n").append(JsoupHtmlUtil.getContent(url + k));
//                }
            });
            File f = new File("D:\\" + bookMap.get("title") + ".txt");
            if (f.exists()) {
                System.out.print("文件存在");
            } else {
                System.out.print("文件不存在");
                if (f.createNewFile()) {
                    System.out.println("新文件创建成功");
                }
            }
            BufferedWriter output = new BufferedWriter(new FileWriter(f));
            output.write(content.toString());
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
